// pages/plantShow/plantShow.js
var util = require('../../utils/util')

let that = null // 页面this指针变量
let db = wx.cloud.database();
const myaudio = wx.createInnerAudioContext({});
Page({

    /**
     * 页面的初始数据
     */
    data: {
        currentPlant:{
            name:"xxx"
        },
        plantsTypeShu:'',
        plantsTypeKe:''
    },
    //在校分部
    gotoZaixiao:function(e){
        // console.log(e)
        let id = e.currentTarget.dataset.id;
        let markPic = e.currentTarget.dataset.markpic;
        let name = e.currentTarget.dataset.name
        // console.log(id)
        // console.log(markPic)
        wx.navigateTo({
          url: '../zaixiao/zaixiao?id=' + id + "&markPic=" + markPic + "&name=" + name,
        })
    },

    //英文发音
    soundEnglish:function(e){
        // console.log(e)
        // console.log(e.currentTarget.dataset)
        let soundAbc = e.currentTarget.dataset.abc
        myaudio.src = soundAbc
        myaudio.play()
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        that = this // 设置页面this指针到全局that

        //读取植物id，查询植物详情
        console.log(options.id)
        let id = options.id
        db.collection('plants').doc(id).get().then(res=>{
            //console.log(res)
            let plant = res.data
            let plantsTypeShu = plant.plantsTypeShu
            //查询属
            db.collection('plantsTypeShu').doc(plantsTypeShu).get().then(res=>{
                // console.log(res)
                that.setData({
                    plantsTypeShu:res.data.name
                })
                let plantsTypeKe = res.data.plantsTypeKe
                db.collection('plantsTypeKe').doc(plantsTypeKe).get().then(res=>{
                    that.setData({
                        plantsTypeKe:res.data.name
                    })
                })
            });
            that.setData({
                currentPlant:plant
            })
            // console.log(that.data.currentPlant.info);
            var newText = util.formatRichText(that.data.currentPlant.info)
            // console.log(newText)
            let newData = that.data.currentPlant;
            newData.info = newText;
            // let newPlant = {info:newText}
            that.setData({
                currentPlant:newData
            })
        }).catch(err=>{
            console.log(err)
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})